package xiaodou.data.account
{
	import com.weibo.charts.ui.dots.NullDot;
	
	import flash.events.EventDispatcher;
	
	import flashx.textLayout.events.DamageEvent;
	
	import xiaodou.core.ObjectUtil;
	import xiaodou.core.WeiboData.WeiboDataDirector;
	import xiaodou.data.blogAccount.BlogAccountData;
	import xiaodou.events.account.AccountEvent;
	import xiaodou.events.blogAccount.BlogAccountEvent;
	
	[Event(name="addAccount", type="xiaodou.events.account.AccountEvent")]
	[Event(name="deleteAccount", type="xiaodou.events.account.AccountEvent")]
	[Event(name="updateAccount", type="xiaodou.events.account.AccountEvent")]
	[Event(name="currentAccountChanged", type="xiaodou.events.account.AccountEvent")]
	
	public class SystemModel extends EventDispatcher
	{
		/**
		 * 系统所有的帐号集合 
		 */		
		private var _accountArr:Array;
		
		private var _blogAccountArr:Array;
		
		private var _currentUser:UserData;
		
		private var _currentBlogAccount:BlogAccountData;
		
		public function SystemModel()
		{
			_accountArr = new Array();
		}
		
		public function addAccount(account:UserData):void
		{
			_accountArr.push(account);
			dispatchEvent(new AccountEvent(AccountEvent.ADD_ACCOUNT, account));
		}
		
		public function removeAccount(id:int):UserData
		{
			var index:int = getAccountIndex(id);
			if(index != -1)
			{
				_accountArr.splice(index, 1);
				dispatchEvent(new AccountEvent(AccountEvent.DELETE_ACCOUNT, _accountArr[index]));
				return _accountArr[index];
			}
			return null;
		}
		
		public function updateAccount(id:int, newData:*):UserData
		{
			//更新帐号数据，如果更新的是当前帐号，需要判断是否需要当前微博
			var account:UserData = getAccount(id);
			if(account != null)
			{
				account.fill(newData);
				if(_currentUser != null && _currentUser.id == account.id)
				{
					_currentUser.fill(newData);
					dispatchEvent(new AccountEvent(AccountEvent.UPDATE_ACCOUNT, _currentUser));
				}
				dispatchEvent(new AccountEvent(AccountEvent.UPDATE_ACCOUNT, account));
				if(_currentUser != null && _currentUser.id == account.id)				//如果是当前帐号，判断当前使用的微博帐号是否仍然包含中此用户的列表中的，如果存在，修改引用；若不存在，修改引用，并且发出微博帐号变化的事件
				{
					if(_currentBlogAccount != null)
					{
						for each (var blogAccount:BlogAccountData in _currentUser.blogAccountList) 
						{
							if(blogAccount.id == _currentBlogAccount.id)
							{
								currentBlogAccount = blogAccount;
								return account;
							}
						}
					}
					currentBlogAccount = _currentUser.blogAccountList.length > 0?_currentUser.blogAccountList[0]:null;
				}
			
			}
			return account;
		}
		
		public function addBlogAccount(blogAccount:BlogAccountData):void
		{
			_blogAccountArr.push(blogAccount);
			dispatchEvent(new BlogAccountEvent(BlogAccountEvent.ADD, blogAccount));
		}
		
		public function updateBlogAccount(data:Object):void
		{
			var account:BlogAccountData = getBlogAccount(data.id);
			if(account)
				account.fill(data);
			//如果当前玩家拥有此微博帐号，也更新
			if(currentUser != null)
			{
				for each(account in currentUser.blogAccountList)
				{
					if(account.id == data.id)
						account.fill(data);
				}
			}
		}
		
		public function getBlogAccount(id:int):BlogAccountData
		{
			for each(var account:BlogAccountData in _blogAccountArr)
			{
				if(account.id == id)
					return account;
			}
			return null;
		}
		
		/**
		 * 删除微博帐号 
		 * @param id
		 * @return 
		 * 
		 */		
		public function deleteBlogAccount(id:int):void
		{
			var accountArr:Array = _currentUser != null?_accountArr.concat(_currentUser):_accountArr;
			var i:int, blogAccount:BlogAccountData;
			//循环所有人的微博帐号列表，删除对应的值
			for each (var user:UserData in accountArr) 
			{
				for (i = 0; i < user.blogAccountList.length; i++) 
				{
					blogAccount = user.blogAccountList[i];
					if(blogAccount.id == id)
					{
						user.blogAccountList.splice(i, 1);
						dispatchEvent(new AccountEvent(AccountEvent.UPDATE_ACCOUNT, user));					//如果删除的是当前用户的微博，发出事件
						break;
					}
				}
			}
			//如果删除的是当前微博
			if(currentBlogAccount != null && currentBlogAccount.id == id)
			{
				currentBlogAccount = _currentUser.blogAccountList.length > 0?_currentUser.blogAccountList[0]:null;
			}
			
			//从微博帐号列表中删除
			for (i = 0; i < _blogAccountArr.length; i++) 
			{
				blogAccount = _blogAccountArr[i];
				if(blogAccount.id == id)
				{
					_blogAccountArr.splice(i, 1);
					dispatchEvent(new BlogAccountEvent(BlogAccountEvent.DELETE, blogAccount));
					break;
				}
			}
			
		}
		
		public function getAccount(id:int):UserData
		{
			var index:int = getAccountIndex(id);
			if(index != -1)
			{
				return _accountArr[index];
			}
			return null;
		}
		
		private function getAccountIndex(id:int):int
		{
			for(var i:int = 0; i < _accountArr.length; i++)
			{
				var account:UserData = _accountArr[i];
				if(account.id == id)
				{
					return i;
				}
			}
			return -1;
		}

		/**
		 * 帐号集合 
		 */
		public function get accountArr():Array
		{
			return _accountArr;
		}

		public function set accountArr(value:Array):void
		{
			_accountArr = value;
			dispatchEvent(new AccountEvent(AccountEvent.ADD_ACCOUNT, null));
		}

		/**
		 * 当前登录系统的帐号
		 */
		public function get currentUser():UserData
		{
			return _currentUser;
		}

		/**
		 * @private
		 */
		public function set currentUser(value:UserData):void
		{
			_currentUser = value;
			if(_currentUser.blogAccountList.length > 0)
				currentBlogAccount = _currentUser.blogAccountList[0];
			dispatchEvent(new AccountEvent(AccountEvent.CURRENT_ACCOUNT_CHANGED, _currentUser));
		}

		/**
		 * 所有微博帐号集合 
		 */
		public function get blogAccountArr():Array
		{
			return _blogAccountArr;
		}

		/**
		 * @private
		 */
		public function set blogAccountArr(value:Array):void
		{
			_blogAccountArr = value;
		}

		/**
		 * 当前使用的微博帐号 
		 */
		public function get currentBlogAccount():BlogAccountData
		{
			return _currentBlogAccount;
		}

		/**
		 * @private
		 */
		public function set currentBlogAccount(value:BlogAccountData):void
		{
			_currentBlogAccount = value;
			if(_currentBlogAccount != null)
			{
				WeiboDataDirector.token = _currentBlogAccount.accessToken;
			}
			dispatchEvent(new BlogAccountEvent(BlogAccountEvent.CURRENT_BLOG_CHANGED, _currentBlogAccount));
		}


	}
}